
require "common/Define"

local Resources = UnityEngine.Resources;
local RectTransformUtility = UnityEngine.RectTransformUtility;
local RectTransform = UnityEngine.RectTransform;


Ball = {x=5, y=5, radius=5};

function Ball:New(o)
	o = o or {};
	setmetatable(o, self);
	self.__index = self;
	return o;
end

function Ball:Draw(canvas)
	-- todo
	local ItemBall = Resources.Load("Prefabs/ItemBall");
	local go = GameObject.Instantiate(ItemBall);
	go.transform:SetParent(canvas);
	self.mBall = go;

	-- set ball local at Input.mousePosition
	local mousePos = Input.mousePosition;
	local screenPos = Vector2.New(mousePos.x, mousePos.y);
	local rect = canvas:GetComponent("RectTransform");
	local hit, localPos = RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, screenPos, nil, nil);
	go.transform.position = localPos;

	local image = go:GetComponent("Image");
	-- image.color = Color.New(math.random(), math.random(), math.random(), 1);
end

function Ball:SetX(posx)
	-- body
	if self.mBall ~= nil then
		local pos = self.mBall.transform.position;
		pos.x = posx;
		self.mBall.transform.position = pos;
	end
end

function Ball:SetY(posy)
	-- body
	if self.mBall ~= nil then
		local pos = self.mBall.transform.position;
		pos.y = posy;
		self.mBall.transform.position = pos;
	end
end